#include "TileEngine.h"

#include "Player.h"
#include "Camera.h"
#include "tinyxml.h"
#include "tinystr.h"

#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"


TileEngine::TileEngine(){

	//	Load textures
	_grass = SGD::GraphicsManager::GetInstance()->LoadTexture(L"resource/graphics/MapTextures/GrassTexture.png");
	_blueflower = SGD::GraphicsManager::GetInstance()->LoadTexture(L"resource/graphics/MapTextures/BlueFlower.png");
	_sand = SGD::GraphicsManager::GetInstance()->LoadTexture(L"resource/graphics/MapTextures/Sand.png");

	m_camera = new Camera();

	m_tiles.empty();
}

TileEngine::~TileEngine(){
	for (unsigned int x = 0; x < m_tiles.size(); ++x)
		delete m_tiles[x];

	m_tiles.clear();

	SGD::GraphicsManager::GetInstance()->UnloadTexture(_grass);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(_blueflower);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(_sand);

	delete m_camera;
}

void TileEngine::Update(float elapsedTime){
	m_camera->Update(elapsedTime, m_worldSize);
}

void TileEngine::Render(){
	
	SGD::GraphicsManager* graphics = SGD::GraphicsManager::GetInstance();

	for (unsigned int x = 0; x < m_tiles.size(); ++x){
		SGD::Point p = SGD::Point(m_tiles.at(x)->GetTileRect().left - m_camera->GetCameraOffset().x, m_tiles.at(x)->GetTileRect().top - m_camera->GetCameraOffset().y);

		graphics->DrawTexture(_grass, p);
	}
}


void TileEngine::LoadMap(const char* filename){

		//	Initialize and load Doc
	TiXmlDocument doc(filename);
	if (!doc.LoadFile()) return;

		//	Doc's handle
	TiXmlHandle hDoc(&doc);

		//	Root of the xml doc
	TiXmlElement* root;
	root = hDoc.ChildElement(0).Element();
	if (!root) return;

		//	Root's child
	TiXmlElement* element = root->FirstChildElement();

		//	Children of element
	element = element->FirstChildElement();

	int tmprows;
	int tmpcols;
	float tmpwidth;
	float tmpheight;

	element->QueryIntAttribute("Rows", &tmprows);
	element->QueryIntAttribute("Cols", &tmpcols);
	element->QueryFloatAttribute("Width", &tmpwidth);
	element->QueryFloatAttribute("Height", &tmpheight);

	SetWorldSize(SGD::Size(tmpwidth, tmpheight));

	element = element->NextSiblingElement();
	while (element != NULL) {

		int tmprow;
		int tmpcol;
		float tmpwidth;
		float tmpheight;
		int tmptype;
		int tmptexture;

		element->QueryIntAttribute("row", &tmprow);
		element->QueryIntAttribute("col", &tmpcol);
		element->QueryFloatAttribute("width", &tmpwidth);
		element->QueryFloatAttribute("height", &tmpheight);
		element->QueryIntAttribute("type", &tmptype);
		element->QueryIntAttribute("texture", &tmptexture);
		
		SGD::Rectangle rect = SGD::Rectangle(tmpcol * tmpwidth, tmprow * tmpheight, (tmpcol * tmpwidth) + tmpwidth, (tmprow * tmpheight) + tmpheight);

		Tile* tmp = new Tile(tmprow, tmpcol, tmptype, tmpwidth, tmpheight, tmptexture, rect);

		m_tiles.push_back(tmp);
		element = element->NextSiblingElement();
	}
}

void TileEngine::SaveTestFile(){

		//	Xml doc Creation
	TiXmlDocument doc;
	TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
	doc.LinkEndChild(decl);

		//	Root of the xml
	TiXmlElement* root = new TiXmlElement("Map");
	doc.LinkEndChild(root);

		//	The root's child
	TiXmlElement* ele = new TiXmlElement("Tiles");
	root->LinkEndChild(ele);

		//	Children of ele
	TiXmlElement* data;

	ROWS = 20;
	COLS = 20;

	data = new TiXmlElement("WorldSettings");
	data->SetAttribute("Rows", ROWS);
	data->SetAttribute("Cols", COLS);
	data->SetAttribute("Width", ROWS * 16);
	data->SetAttribute("Height", COLS * 16);
	ele->LinkEndChild(data);

	for (unsigned int r = 0; r < ROWS; ++r) {
		for (unsigned int c = 0; c < COLS; ++c) {

			data = new TiXmlElement("Tile");
			data->SetAttribute("col", c);
			data->SetAttribute("row", r);
			data->SetAttribute("width", 16);
			data->SetAttribute("height", 16);
			data->SetAttribute("type", 0);
			data->SetAttribute("texture", 0);
			ele->LinkEndChild(data);
		}
	}
	doc.SaveFile("testmap.xml");
}